home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / OS / ZPatch.h < prev    next >
Text File  |  1997-06-18  |  3KB  |  104 lines

  1. /*
  2.  *  File:       ZPatch.h
  3.  *  Summary:       Classes to help with patching traps.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *    Classes:    TTrapPatch            - Base class for patching traps.
  10.  *                TPatchExitToShell    - Subclass used to patch ExitToShell.
  11.  *
  12.  *    Usage:        To patch a trap create a subclass of TTrapPatch (see 
  13.  *                TPatchExitToShell for an example of how to do this).
  14.  *                Once you have the subclass do something like this:
  15.  *
  16.  *                static TPatchExitToShell sExitToShellPatch;
  17.  *
  18.  *                #pragma segment Main
  19.  *                #pragma profile off
  20.  *                static pascal void OnExitToShell()
  21.  *                {
  22.  *                    long OldA5 = SetCurrentA5();                // ***** Called from trap patches *****
  23.  *
  24.  *                    sExitToShellPatch.Remove();                    // guaranteed not to fail
  25.  *
  26.  *                    DebugNewReportLeaks();
  27.  *
  28.  *                    sExitToShellPatch.CallRemoved();
  29.  *
  30.  *                    SetA5(OldA5);
  31.  *                }
  32.  *                #pragma profile reset
  33.  *
  34.  *                sExitToShellPatch.Install(OnExitToShell);
  35.  *
  36.  *                Note that for most traps you won't remove the patch
  37.  *                in your replacement function.
  38.  *
  39.  *  Change History (most recent first):    
  40.  *
  41.  *         <->     1/13/96    JDJ        Created (based on MacApp's class).
  42.  */
  43.  
  44. #pragma once
  45.  
  46. #include <ZTypes.h>
  47.  
  48. typedef class TTrapPatch *TrapPatchPtr;
  49.  
  50.  
  51. // ===================================================================================
  52. //    class TTrapPatch
  53. // ===================================================================================
  54. class TTrapPatch {
  55.  
  56. public:
  57.                         TTrapPatch();
  58.             
  59.             void         PatchTrap(ushort theTrapNum, void* theRoutine);
  60.                         // Patches the trap so that it calls routine.
  61.             
  62.             void         UnpatchTrap();
  63.     
  64.     static     void         UnpatchAll();
  65.                         
  66.             UniversalProcPtr GetOldTrapAddr() const    {return mOldTrapAddr;}
  67.     
  68. protected:
  69.             TrapPatchPtr GetPreviousPatchPtr() const;
  70.     
  71.             TrapPatchPtr GetNewerPatchPtr() const;
  72.     
  73. protected:
  74.     ushort                mTrapNum;            // trap # being patched
  75.     UniversalProcPtr    mOldTrapAddr;        // old trap address
  76.     UniversalProcPtr    mPatchRoutine;        // new trap routine descriptor (created by subclasses
  77.                                             // of class TTrapPatch, cleaned up by TTrapPatch)
  78.     TrapPatchPtr        mNextPatch;            // next link in linked list of patches
  79.     
  80.     static TrapPatchPtr msPatchList;
  81. };
  82.  
  83.  
  84. // ===================================================================================
  85. //    class TPatchExitToShell
  86. // ===================================================================================
  87. typedef pascal void (*ExitToShellType)(void);
  88.     
  89. class TPatchExitToShell : private TTrapPatch {
  90.  
  91. public:
  92.                         TPatchExitToShell()                        {mRemovedTrapAddr = nil;}
  93.  
  94.             void         Install(ExitToShellType routine);
  95.             void         Remove();
  96.             void         CallRemoved();
  97.             
  98.             bool        Installed()                                {return mPatchRoutine != nil;}
  99.  
  100. private:
  101.     UniversalProcPtr    mRemovedTrapAddr;            
  102. };
  103.  
  104.